package com.bdqn.zhang.controller;

import com.bdqn.zhang.mapper.GradeDao;
import com.bdqn.zhang.pojo.Grade;
import com.bdqn.zhang.service.GradeService;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <Description> <br>
 *
 * @author tao<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2019/09/20 15:35 <br>
 * @see com.bdqn.controller <br>
 */
@Controller
@RequestMapping("/grade")
public class GradeController {

    @Resource
    private GradeService gradeService;

    @Resource
    private GradeDao gradeDao;

    @RequestMapping(value="/toAdd",method = RequestMethod.GET)
    public String toUserList(Model model, HttpSession session){
        return "gradeAdd";
    }


    @RequestMapping(value="/addSave",method = RequestMethod.POST)
    public String addSave(Grade grade){
        Grade g = gradeService.addGrade(grade);
        System.out.println(g);
        return "success";
    }

    @RequestMapping(value="/addSaveList",method = RequestMethod.GET)
    public String addSaveList(){
        ArrayList<Grade> list = new ArrayList<>();
        Grade g1 = new Grade();
        g1.setGname("小学五年级");
        Grade g2 = new Grade();
        g2.setGname("小学六年级");
        list.add(g1);
        list.add(g2);
        List<Grade> grades= gradeDao.saveAll(list);
        return "success";
    }


    /**
     * springboot 1.x 中 findOne 可以直接传入id
     * @return
     */
    @RequestMapping(value="/findOne",method = RequestMethod.GET)
    public String findOne(){
        Grade grade = new Grade();
        grade.setId(3L);
        Example<Grade> example = Example.of(grade);
        Optional<Grade> gradeOptional = gradeDao.findOne(example);
        if (gradeOptional.isPresent()) {
            Grade result = gradeOptional.get();
            System.out.println(result.toString());
        } else {
            // handle not found, return null or throw
            System.out.println("no exit!");
        }
        return "success";
    }

    /**
     * @return
     */
    @RequestMapping(value="/findById",method = RequestMethod.GET)
    public String findById(){
        Optional<Grade> gradeOptional = gradeDao.findById(3L);
        return "success";
    }


    /**
     *  关键字抽象方法
     *  根据年级名称查询数据
     */
    @RequestMapping("/findByGnameContaining")
    public Object findByGnameContaining(String gname) {
        List<Grade> grades = gradeDao.findByGnameContaining(gname);
        System.out.println(grades.size());
        return "success";
    }


    /**
     * 使用@Query自定义查询
     */
    @RequestMapping("/findByGname")
    public Object findByGname(String gname) {
        Grade grade = gradeDao.findByGname(gname);
        System.out.println(grade.getId());
        return "success";
    }

    /**
     * 使用@Query自定义更新
     */
    @RequestMapping("/updateGradeName")
    public Object updateGradeName(String gname,Long id) {
        int num = gradeDao.updateGradeName(gname,id);
        return "success";
    }

}
